<template>
  <div>
      <el-input class="inputAdd" v-model="name" placeholder="设置用户名"></el-input>
      <el-input class="inputAdd" v-model="pwd" placeholder="设置密码"></el-input>
      <el-row style="margin-top:10px">
          <el-col :span="12">
            设置权限:
            <Authority @authorities="getAuthorities"></Authority>
            <el-button type="success" @click="add">提交</el-button>
          </el-col>
          <el-col :span="12" style="margin-top:10px">
              功能权限：
              <div style="margin-top:10px">
                  <el-checkbox-group v-model="roles">
                        <el-checkbox style="display:block;width:20%;margin-left:10px;margin-bottom:10px" label="知识之库" border></el-checkbox>
                        <el-checkbox style="display:block;width:20%;margin-bottom:10px" label="产品目录" border></el-checkbox>
                        <el-checkbox style="display:block;width:20%;margin-bottom:10px" label="系统管理" border></el-checkbox>
                        <el-checkbox style="display:block;width:20%;margin-bottom:10px" label="订单管理" border></el-checkbox>
                        <el-checkbox style="display:block;width:20%;margin-bottom:10px" label="费用预算" border></el-checkbox>
                        <el-checkbox style="display:block;width:20%;margin-bottom:10px" label="汇率管理" border></el-checkbox>
                  </el-checkbox-group>
              </div>
          </el-col>
      </el-row>
  </div>
</template>
<script>
import Authority from './component/Authority'

export default {
  components:{
      Authority
  },
  data(){
      return {
          name:'',
          pwd:'',
          userPermission:[], //用户权限
          type:'P', //用户类型
          roles:[]
      }
  },
  methods:{
      getAuthorities(data){
        this.userPermission = data;
      },
      add(){
        if(this.name == ''&& this.pwd == ''){
            this.$message.error('用户名或密码不能为空')
        }else{
            if(this.userPermission.length == 0){
              this.$message.error('请至少选择一个权限')
            }else if(this.roles == []){
              this.$message.error('请至少选择一个功能')
            }else{
                var params = new URLSearchParams();
                params.append('permission', this.userPermission);
                params.append('name', this.name);
                params.append('pwd', this.pwd);
                params.append('type',this.type)
                params.append('role',JSON.stringify(this.roles))
                // console.log(`${this.name}/${this.pwd}/${this.userPermission}`)
                this.$http.post("http://"+this.IP+"/api/addUser.php",params)
                    .then((res)=>{
                    if(res.data.code == 20000){
                        this.$message.success(res.data.msg)
                        this.$router.replace('/admin/adminUser')
                    }else{
                        this.$message.error(res.data.msg);
                    }
                })
            }
        }
      }
  }
}
</script>
<style scoped>
    div{
        /* width: 1000px */
    }
    .inputAdd{
        width: 200px
    }
 </style>
